﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace FinWeb3.Models.Repository
{
    public class StatementRepository : IRepository<Statement>
    {
        public void Save(Statement item, System.Security.Principal.IIdentity user)
        {
            if (item.Id == 0)
            {
                item.UserName = user.Name;
                item.Save();
            }
            else
            {
                var statement = Get(item.Id, user);

                if (statement != null)
                {
                    statement.BankId = item.BankId;
                    statement.Day = item.Day;
                    statement.Description = item.Description;
                    statement.FitId = item.FitId;
                    statement.UserName = user.Name;
                    statement.Value = item.Value;

                    statement.Save();
                }
            }
        }

        public void Delete(Statement item, System.Security.Principal.IIdentity user)
        {
            var statement = Get(item.Id, user);

            if (statement != null)
                statement.Delete();
        }

        public Statement Get(int id, System.Security.Principal.IIdentity user)
        {
            return Statement.All().Where(x => x.UserName == user.Name && x.Id == id).FirstOrDefault();
        }

        public IQueryable<Statement> GetAll(System.Security.Principal.IIdentity user)
        {
            return Statement.All().Where(x => x.UserName == user.Name);
        }
    }
}
